<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <el-table :data="artData" border style="width: 100%">
        <el-table-column prop="id" label="序号"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="getArt(row.id)"
              >修改</el-button
            >
            <el-button size="mini" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 本项目复用的组件，其他项目 -->
    <AddList
      :dialogVisible="dialogVisible"
      @dialogVisibleBtn="dialogVisibleBtn"
      @addList="addList"
      :ruleForm="ruleForm"
      @updateList="updateList"
    ></AddList>
  </div>
</template>

<script>
// 导入网络请求
import {
  getList,
  addList,
  getListInfo,
  updateListInfo,
  delArtInfo
} from 'network/menus'

// 子组件
import AddList from 'components/content/addArtList/AddList'

export default {
  name: 'ArtCate',
  data () {
    return {
      artData: [],
      dialogVisible: false,
      ruleForm: {
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  components: {
    AddList
  },
  methods: {
    // 获取所有分类列表
    async getartData () {
      const { data: res } = await getList()
      if (res.code !== 0) return
      this.artData = res.data
    },
    // 小窗口的打开
    dialogVisibleBtn (val) {
      this.dialogVisible = val
    },
    // 添加分类列表
    async addList (data) {
      const { data: res } = await addList(data)
      if (res.code !== 0) return this.$message.error(res.message)
      this.getartData()
      this.$message.success(res.message)
      this.dialogVisible = false
    },
    // 根据id获取分类列表
    async getArt (id) {
      console.log(id)
      const { data: res } = await getListInfo({ id })
      if (res.code !== 0) return this.$message.error(res.message)
      this.ruleForm = res.data
      this.dialogVisible = true
      this.$message.success(res.message)
    },
    // 更新分类列表(重置有bug，待修复)
    async updateList (data) {
      const { data: res } = await updateListInfo(data)
      if (res.code !== 0) return this.$message.error(res.message)
      this.getartData()
      this.$message.success(res.message)
      this.dialogVisible = false
    },
    // 删除
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await delArtInfo({ id })
        if (res.code !== 0) return this.$message.error(res.message)
        this.getartData()
        this.$message.success(res.message)
      }).catch(() => {
      })
    }
  },
  created () {
    this.getartData()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
